﻿using System;
using System.Web.Security;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;
using Resources;

namespace MonkeyWarranty.Web.Employees
{
    public partial class EditProfile : System.Web.UI.Page
    {
        #region Properties

        private EmployeeBiz _employeeBiz;

        private EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        private static Employee CurrentEmployee
        {
            get { return WarrantyCommon.CurrrentEmployee; }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.Admin))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!IsPostBack)
            {
                // Load data
                //txtCode.Text = CurrentEmployee.Code;
                txtName.Text = CurrentEmployee.Name;
                txtUserName.Text = CurrentEmployee.UserName;
                txtEmailAddress.Text = CurrentEmployee.MailAddress;
                txtPhoneNumber.Text = CurrentEmployee.PhoneNumber;
                txtDescription.Text = CurrentEmployee.Description;
                txtPassword.Text = string.Empty;
                //txtSortOrder.Text = CurrentEmployee.SortOrder.ToString(CultureInfo.InvariantCulture);
                //chkHidden.Checked = CurrentEmployee.Hidden;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;
            // validate inputs
            //var code = txtCode.Text.Trim();
            //if (code.Length == 0)
            //{
            //    lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
            //    return;
            //}
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var email = txtEmailAddress.Text.Trim();
            var phone = txtPhoneNumber.Text.Trim();
            var descrition = txtDescription.Text;
            //int sortOrder;
            //int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            //var isHidden = chkHidden.Checked;

            //CurrentEmployee.Code = code;
            CurrentEmployee.Name = name;
            CurrentEmployee.MailAddress = email;
            CurrentEmployee.PhoneNumber = phone;
            CurrentEmployee.Description = descrition;
            if (txtPassword.Text.Length > 0)
            {
                if (txtPassword.Text != txtConfirmPassword.Text)
                {
                    lblErrorMessage.Text = "Mật khẩu và xác nhận mật khẩu không chính xác!";
                    errorMessage.Visible = true;
                    return;
                }
                CurrentEmployee.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");
            }
            //if (sortOrder > 0)
            //    CurrentEmployee.SortOrder = sortOrder;
            //CurrentEmployee.Hidden = isHidden;

            // update
            EmployeeManager.UpdateEmployee(CurrentEmployee);

            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
            errorMessage.Visible = true;
            //Response.Redirect("List.aspx");
        }

        #endregion
    }
}